﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Business
{
    public class PhieuNhapKho
    {

        public int MaPNK { get; set; }

        public string MaPNKHienThi
        {
            get
            {

                return "PNK" + MaPNK;
            }
        }
        public DateTime NgayNhap { get; set; }
        public int MaHDNhap { get; set; }
        public string MaHDNhapHienThi
        {
            get
            {
                return "HDM" + MaHDNhap;
            }
        }
        public double TongGT { get; set; }
        public int MaNV { get; set; }
        public string MaNVHienThi
        {
            get
            {

                return "NV" + MaNV;
            }
        }
        public string LyDoNhap { get; set; }

        public List<ChiTietPhieuNhapKho> DanhSachChiTietPhieuNhap { get; set; }

        public PhieuNhapKho(int maHDN, double tongtien)
        {
            this.MaPNK = 0;
            this.MaHDNhap = maHDN;
            this.LyDoNhap = "Nhập theo đơn: HDM" + maHDN;
            this.TongGT = tongtien;
        }

        public PhieuNhapKho(int maPNK, DateTime ngayNhap,
            int maHDNhap, double tongGT, int maNV, string liDoNhap)
        {
            this.MaPNK = maPNK;
            this.NgayNhap = ngayNhap;
            this.MaHDNhap = maHDNhap;
            this.TongGT = tongGT;
            this.MaNV = maNV;
            this.LyDoNhap = liDoNhap;
        }

        public static PhieuNhapKho TaoPhieuNhapKho(int maHDM)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
            PhieuNhapKho phieuNhapKho;
            List<ChiTietPhieuNhapKho> DanhSachChiTietPhieuNhap;
            try
            {
                phieuNhapKho = (from p in db.PhieuNhapKhos
                                where p.MaHDNhap == maHDM
                                select new PhieuNhapKho(p.MaPNK, p.NgayNhap, p.MaHDNhap, p.TongGT, p.MaNV, p.LiDoNhap)).Single();

                DanhSachChiTietPhieuNhap = (from p in db.ChiTietPhieuNhapKhos
                                            where p.MaPNK == phieuNhapKho.MaPNK
                                            select new ChiTietPhieuNhapKho(p.MaCTPNK, p.MaPNK, p.MaHang, p.HangHoa.TenHang, p.SoLuong, p.DonGiaNhap, p.HanSuDung)).ToList();
            }
            catch
            {
                phieuNhapKho = (from p in db.HoaDonNhaps
                                where p.MaHDNhap == maHDM
                                select new PhieuNhapKho(p.MaHDNhap, p.TongGT)).Single();

                DanhSachChiTietPhieuNhap = (from p in db.ChiTietHDNhaps
                                            where p.MaHDNhap == maHDM
                                            select new ChiTietPhieuNhapKho(phieuNhapKho.MaPNK, p.MaHang, p.HangHoa.TenHang, p.SoLuong, p.GiaNhap)).ToList();
            }

            if (DanhSachChiTietPhieuNhap == null || DanhSachChiTietPhieuNhap.Count == 0)
                DanhSachChiTietPhieuNhap = new List<ChiTietPhieuNhapKho>();

            phieuNhapKho.DanhSachChiTietPhieuNhap = DanhSachChiTietPhieuNhap;
            return phieuNhapKho;
        }

        public static PhieuNhapKho LayPhieuNhapKho(int maPNK)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
            PhieuNhapKho phieuNhapKho = (from p in db.PhieuNhapKhos
                                         where p.MaPNK == maPNK
                                         select new PhieuNhapKho(p.MaPNK, p.NgayNhap, p.MaHDNhap, p.TongGT, p.MaNV, p.LiDoNhap)).Single();

            List<ChiTietPhieuNhapKho> DanhSachChiTietPhieuNhap = (from p in db.ChiTietPhieuNhapKhos
                                                                  where p.MaPNK == maPNK
                                                                  select new ChiTietPhieuNhapKho(p.MaCTPNK, p.MaPNK, p.MaHang, p.HangHoa.TenHang, p.SoLuong, p.DonGiaNhap, p.HanSuDung)).ToList();

            if (DanhSachChiTietPhieuNhap == null || DanhSachChiTietPhieuNhap.Count == 0)
                DanhSachChiTietPhieuNhap = new List<ChiTietPhieuNhapKho>();

            phieuNhapKho.DanhSachChiTietPhieuNhap = DanhSachChiTietPhieuNhap;

            return phieuNhapKho;
        }

        public static List<PhieuNhapKho> LayDanhSach()
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
            return (from p in db.PhieuNhapKhos
                    select new PhieuNhapKho(p.MaPNK, p.NgayNhap, p.MaHDNhap, p.TongGT, p.MaNV, p.LiDoNhap)).ToList();
        }

        public static List<PhieuNhapKho> LayDanhSach_MaHoaDonMua(string maHDMHienThi)
        {
            try
            {
                int maHDM = Int16.Parse(maHDMHienThi.Substring(3));

                DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
                return (from p in db.PhieuNhapKhos
                        where p.MaHDNhap == maHDM
                        select new PhieuNhapKho(p.MaPNK, p.NgayNhap, p.MaHDNhap, p.TongGT, p.MaNV, p.LiDoNhap)).ToList();
            }
            catch
            {
                return LayDanhSach();
            }
        }

        public static List<PhieuNhapKho> LayDanhSach_NgayLapPhieu(DateTime tungay, DateTime denngay)
        {
            try
            {
                DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
                return (from p in db.PhieuNhapKhos
                        where p.NgayNhap.Date >= tungay && p.NgayNhap.Date <= denngay
                        select new PhieuNhapKho(p.MaPNK, p.NgayNhap, p.MaHDNhap, p.TongGT, p.MaNV, p.LiDoNhap)).ToList();
            }
            catch
            {
                return LayDanhSach();
            }
        }

        public static List<PhieuNhapKho> LayDanhSach_MaPhieuNhap(string maPNKHienThi)
        {
            try
            {
                int maPNK = Int16.Parse(maPNKHienThi.Substring(3));
                DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

                return (from p in db.PhieuNhapKhos
                        where p.MaPNK == maPNK
                        select new PhieuNhapKho(p.MaPNK, p.NgayNhap, p.MaHDNhap, p.TongGT, p.MaNV, p.LiDoNhap)).ToList();
            }
            catch
            {
                return LayDanhSach();
            }
        }

        public static int ThemMoi(PhieuNhapKho phieuNhapKho)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

            DataAccess.PhieuNhapKho tamObject = new DataAccess.PhieuNhapKho();
            tamObject.MaNV = phieuNhapKho.MaNV;
            tamObject.MaHDNhap = phieuNhapKho.MaHDNhap;
            tamObject.TongGT = phieuNhapKho.TongGT;

            tamObject.NgayNhap = phieuNhapKho.NgayNhap;

            tamObject.LiDoNhap = phieuNhapKho.LyDoNhap;

            db.PhieuNhapKhos.InsertOnSubmit(tamObject);
            db.SubmitChanges();

            foreach (var chitietPNK in phieuNhapKho.DanhSachChiTietPhieuNhap)
            {
                DataAccess.ChiTietPhieuNhapKho chiTietTamObject = new DataAccess.ChiTietPhieuNhapKho();
                chiTietTamObject.MaPNK = tamObject.MaPNK;

                chiTietTamObject.MaHang = chitietPNK.MaHang;
                chiTietTamObject.SoLuong = chitietPNK.SoLuong;

                chiTietTamObject.DonGiaNhap = chitietPNK.DonGiaNhap;
                chiTietTamObject.HanSuDung = chitietPNK.HanSuDung;

                db.ChiTietPhieuNhapKhos.InsertOnSubmit(chiTietTamObject);
            }

            for (int i = 0; i < phieuNhapKho.DanhSachChiTietPhieuNhap.Count; i++)
            {
                int mahang = phieuNhapKho.DanhSachChiTietPhieuNhap[i].MaHang;
                var hangHoa = (from p in db.HangHoas
                               where p.MaHang == mahang
                               select p).Single();
                int soLuongNhap = phieuNhapKho.DanhSachChiTietPhieuNhap[i].SoLuong;
                if (soLuongNhap > 0)
                {
                    hangHoa.SoLuongTon = hangHoa.SoLuongTon + soLuongNhap;
                }
            }
            db.SubmitChanges();
            return tamObject.MaPNK;
        }

        private bool TonTaiChiTietPNK(int maCTPNK)
        {
            foreach (var chitietPNK in this.DanhSachChiTietPhieuNhap)
            {
                if (chitietPNK.MaCTPNK == maCTPNK)
                    return true;
            }
            return false;
        }


        public static void CapNhat(PhieuNhapKho phieuNhapKho)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

            var tamObject = (from p in db.PhieuNhapKhos
                             where p.MaPNK == phieuNhapKho.MaPNK
                             select p).Single();

            tamObject.NgayNhap = phieuNhapKho.NgayNhap;
            tamObject.MaHDNhap = phieuNhapKho.MaHDNhap;
            tamObject.TongGT = phieuNhapKho.TongGT;
            tamObject.LiDoNhap = phieuNhapKho.LyDoNhap;

            foreach (var chiTietTamObject in tamObject.ChiTietPhieuNhapKhos)
            {
                if (!phieuNhapKho.TonTaiChiTietPNK(chiTietTamObject.MaCTPNK))
                    db.ChiTietPhieuNhapKhos.DeleteOnSubmit(chiTietTamObject);
            }


            foreach (var chiTietPNK in phieuNhapKho.DanhSachChiTietPhieuNhap)
            {
                if (chiTietPNK.MaCTPNK == 0)
                {
                    DataAccess.ChiTietPhieuNhapKho chiTietTamObject = new DataAccess.ChiTietPhieuNhapKho();
                    chiTietTamObject.MaPNK = tamObject.MaPNK;

                    chiTietTamObject.MaHang = chiTietPNK.MaHang;
                    chiTietTamObject.SoLuong = chiTietPNK.SoLuong;

                    chiTietTamObject.DonGiaNhap = chiTietPNK.DonGiaNhap;
                    chiTietTamObject.HanSuDung = chiTietPNK.HanSuDung;

                    db.ChiTietPhieuNhapKhos.InsertOnSubmit(chiTietTamObject);
                }
                else
                {
                    var chiTietTamObject = (from p in db.ChiTietPhieuNhapKhos
                                            where p.MaCTPNK == chiTietPNK.MaCTPNK
                                            select p).Single();

                    chiTietTamObject.MaPNK = tamObject.MaPNK;

                    chiTietTamObject.MaHang = chiTietPNK.MaHang;
                    chiTietTamObject.SoLuong = chiTietPNK.SoLuong;

                    chiTietTamObject.DonGiaNhap = chiTietPNK.DonGiaNhap;
                    chiTietTamObject.HanSuDung = chiTietPNK.HanSuDung;

                }
            }

            for (int i = 0; i < phieuNhapKho.DanhSachChiTietPhieuNhap.Count; i++)
            {
                int mahang = phieuNhapKho.DanhSachChiTietPhieuNhap[i].MaHang;
                var hangHoa = (from p in db.HangHoas
                               where p.MaHang == mahang
                               select p).Single();
                int soLuongNhap = phieuNhapKho.DanhSachChiTietPhieuNhap[i].SoLuong;
                if (soLuongNhap > 0)
                {
                    hangHoa.SoLuongTon = hangHoa.SoLuongTon + soLuongNhap;
                }
            }

            db.SubmitChanges();
        }

    }
}
